package test.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.validation.BindingResult;
import java.util.Map;
import javax.validation.Valid;

import test.model.LoginForm;
import test.services.LoginService;

@Controller
@RequestMapping("user.htm")
public class LoginController {

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
        LoginForm loginForm = new LoginForm();
        model.put("loginForm", loginForm);
        return "loginform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(@Valid LoginForm loginForm, BindingResult result,
            Map model) {

        LoginService ls = new LoginService();

        String userName = loginForm.getUserName();
        String password = loginForm.getPassword();
        String type = loginForm.getType();

        if (result.hasErrors()) {
            return "../../index";
        }

        int num = ls.loginValidation(userName, password,type);
        loginForm = (LoginForm) model.get("loginForm");
        if (num == 1 ) {
            return "p_user";
        }
        else if(num==2){
            return "road-network";
        }
        else 
            return "../../index";
        
    }
}
